package cn.com.chec.en.controller.front;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import cn.com.chec.en.dao.ChannelInfoDao;
import cn.com.chec.en.dao.NewsInfoDao;
import cn.com.chec.en.dao.PageLinkDao;
import cn.com.chec.en.model.domain.ChannelInfo;
import cn.com.chec.en.model.domain.NewsInfo;
import cn.com.chec.en.model.domain.QueryNewsObject;
import cn.com.chec.en.model.dto.NewsDTO;
import cn.com.chec.en.util.ContactUtils;
import cn.com.chec.en.util.PageBean;

@Controller
public class AboutUsFrontController {
	protected static final Logger logger = Logger
			.getLogger(AboutUsFrontController.class);

	@Resource
	private PageLinkDao pageLinkDao;

	@Resource
	private ChannelInfoDao channelInfoDao;

	@Resource
	private NewsInfoDao newsInfoDao;

	@Resource
	private Map<String, Object> environment;

	@RequestMapping(value = { "/aboutus" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET })
	public ModelAndView index(
			@RequestParam(value = "chId", required = false) String channelId,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap map) throws Exception {
		ModelAndView mav = new ModelAndView("aboutus");

		map.put("pageLinkMap", this.pageLinkDao.getAllAsMap());

		map.putAll(ContactUtils.getContactMap());

		int defaultChannelId = 6;
		List<ChannelInfo> channelList = this.channelInfoDao.getSubListByParentIdLanguageId(
				1, defaultChannelId, 0, 10000);
		if ((channelList != null) && (channelList.size() > 0)) {
			defaultChannelId = ((ChannelInfo) channelList.get(0))
					.getChannelId();
		}
		map.put("channelList", channelList);

		int chId = (StringUtils.isNotBlank(channelId))
				&& (channelId.matches("\\d+")) ? Integer.valueOf(channelId)
				.intValue() : defaultChannelId;
		NewsDTO dto = this.newsInfoDao.getNewsDTOByChannelId(chId);
		ChannelInfo channelInfo = dto.getRel() != null ? dto.getRel()
				.getChannelnfo()
				: dto.getRel().getChannelnfo() == null ? new ChannelInfo()
						: new ChannelInfo();
		NewsInfo newsInfo = dto.getNewsInfo() != null ? dto.getNewsInfo()
				: new NewsInfo();

		map.put("channelInfo", channelInfo);
		map.put("newsInfo", newsInfo);

		map.putAll(this.environment);
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping({ "/aboutus/cr/{chId}" })
	public ModelAndView cr(@PathVariable("chId") String channelId,
			@RequestParam(value = "curPage", required = false) String curPage,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap map) throws Exception {
		ModelAndView mav = new ModelAndView("aboutus-cr");

		map.put("pageLinkMap", this.pageLinkDao.getAllAsMap());

		map.putAll(ContactUtils.getContactMap());

		int defaultChannelId = 6;
		List<ChannelInfo> channelList = this.channelInfoDao.getSubListByParentIdLanguageId(
				1, defaultChannelId, 0, 10);
		if ((channelList != null) && (channelList.size() > 0)) {
			defaultChannelId = ((ChannelInfo) channelList.get(0))
					.getChannelId();
		}
		map.put("channelList", channelList);

		int chId = (StringUtils.isNotBlank(channelId))
				&& (channelId.matches("\\d+")) ? Integer.valueOf(channelId)
				.intValue() : defaultChannelId;
		ChannelInfo channelInfo = this.channelInfoDao
				.getChannelByChannelId(chId);

		PageBean pageBean = new PageBean();
		pageBean.setRowsPerPage(6);
		int curPageInt = (StringUtils.isNotBlank(curPage))
				&& (curPage.matches("\\d+")) ? Integer.parseInt(curPage) : 1;
		pageBean.setCurPage(curPageInt);
		List<ChannelInfo> crClList = this.channelInfoDao.getSubListByParentIdLanguageId(1,
				chId, pageBean.getStart(), pageBean.getRowsPerPage());
		pageBean.setMaxRowCount(this.channelInfoDao
				.getCountByParentIdLanguageId(1, chId));
		pageBean.setMaxPage();
		pageBean.setPageNoList();

		map.put("channelInfo", channelInfo);
		map.put("crChannelList", crClList);
		map.put("pageBean", pageBean);

		map.putAll(this.environment);
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping({ "/aboutus/crNews/{chId}" })
	public ModelAndView crDetail(@PathVariable("chId") String channelId,
			@RequestParam(value = "curPage", required = false) String curPage,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap map) throws Exception {
		ModelAndView mav = new ModelAndView("crDetail");

		map.put("pageLinkMap", this.pageLinkDao.getAllAsMap());

		map.putAll(ContactUtils.getContactMap());

		int chId = 0;
		if ((StringUtils.isNotBlank(channelId)) && (channelId.matches("\\d+"))) {
			chId = Integer.valueOf(channelId).intValue();
		}

		List<ChannelInfo> channelList = null;
		ChannelInfo clInfo = this.channelInfoDao.getChannelByChannelId(chId);
		ChannelInfo pInfo = this.channelInfoDao.getChannelByChannelId(clInfo
				.getChannelParentId());
		if (clInfo.getHasChild() == 1) {
			channelList = this.channelInfoDao.getSubListByParentIdLanguageId(1,
					chId, 0, 10);
		} else if (clInfo.getChannelParentId() != 2) {
			channelList = this.channelInfoDao.getSubListByParentIdLanguageId(1,
					clInfo.getChannelParentId(), 0, 2147483647);
		} else {
			channelList = this.channelInfoDao.getSubListByParentIdLanguageId(1,
					2, 0, 2147483647);
		}
		map.put("clInfo", clInfo);
		map.put("pInfo", pInfo);
		map.put("channelList", channelList);

		PageBean pageBean = new PageBean();
		int curPageInt = (StringUtils.isNotBlank(curPage))
				&& (curPage.matches("\\d+")) ? Integer.parseInt(curPage) : 1;
		pageBean.setCurPage(curPageInt);
		QueryNewsObject q = new QueryNewsObject();
		q.setLanguageId(1);
		q.setIsShowInList(1);
		q.setStart(pageBean.getStart());
		q.setNum(pageBean.getRowsPerPage());
		List<NewsDTO> newsList = this.newsInfoDao.getNewsListByQueryPChannelIdDistinct(
				q, chId);
		pageBean.setMaxRowCount(this.newsInfoDao
				.getNewsCountByQueryPChannelIdDistinct(q, chId));
		pageBean.setMaxPage();
		pageBean.setPageNoList();

		map.put("newsInfoDTOList", newsList);
		map.put("pageBean", pageBean);

		map.putAll(this.environment);
		mav.addAllObjects(map);
		return mav;
	}
}